package com.fh.web.controller;

import com.fh.common.annotation.AutoLog;
import com.fh.common.api.vo.Result;
import com.fh.common.constant.CommonConstant;
import com.fh.common.web.AbstractController;

import com.fh.modules.ucas.entity.SysDepart;
import com.fh.modules.ucas.model.DepartIdModel;
import com.fh.modules.ucas.model.SysDepartTreeModel;
import com.fh.web.FindsDepartsChildrenUtil;
import com.fh.web.service.ISysDepartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Qiu,Junda
 * @date 2019年4月3日
 * @description 部门表 前端控制器
 */
@RestController
@RequestMapping("/sysdepart/sysDepart")
@Api("部门组织机构")
public class SysDepartController extends AbstractController {

	@Autowired
	private ISysDepartService sysDepartService;

	/**
	 * 查询数据 查出所有部门,并以树结构数据格式响应给前端
	 * 
	 * @return
	 */
	@RequestMapping(value = "/queryTreeList", method = RequestMethod.GET)
	@ApiOperation("查询组织机构部门树")
	@AutoLog(value="查询组织机构部门树",logType=CommonConstant.LOG_TYPE_QUERY)
	public Result<List<SysDepartTreeModel>> queryTreeList() {
		Result<List<SysDepartTreeModel>> result = new Result<>();
		try {
			List<SysDepartTreeModel> list = sysDepartService.queryTreeList();
			result.setResult(list);
			result.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 添加新数据 添加用户新建的部门对象数据,并保存到数据库
	 * 
	 * @param sysDepart
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
//	@RequiresPermissions("sys:depart:create")
	@ApiOperation("新增组织机构部门信息")
	@AutoLog("新增组织机构部门信息")
	public Result<SysDepart> add(@RequestBody SysDepart sysDepart, HttpServletRequest request) {
		Result<SysDepart> result = new Result<SysDepart>();
		String username = this.getUserAccount();
		try {
			sysDepart.setCreateBy(username);
			sysDepartService.saveDepartData(sysDepart, username);
			result.success("添加成功！");
		} catch (Exception e) {
			e.printStackTrace();
			log.info(e.getMessage());
			result.error500("操作失败");
		}
		return result;
	}

	/**
	 * 编辑数据 编辑部门的部分数据,并保存到数据库
	 * 
	 * @param sysDepart
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.PUT)
//	@RequiresPermissions("sys:depart:update")
	@ApiOperation("修改组织机构部门信息")
	@AutoLog("修改组织机构部门信息")
	public Result<SysDepart> eidt(@RequestBody SysDepart sysDepart, HttpServletRequest request) {

		String username = this.getUserAccount();
		sysDepart.setUpdateBy(username);
		Result<SysDepart> result = new Result<SysDepart>();
		SysDepart sysDepartEntity = sysDepartService.getById(sysDepart.getId());
		if (sysDepartEntity == null) {
			result.error500("未找到对应实体");
		} else {
			boolean ok = sysDepartService.updateDepartDataById(sysDepart, username);
			if (ok) {
				result.success("修改成功!");
			}
		}
		return result;
	}

	/**
	 * 通过id删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
//	@RequiresPermissions("sys:depart:delete")
	@ApiOperation("删除组织机构部门")
	@AutoLog("删除组织机构部门")
	public Result delete(@RequestParam(name = "id", required = true) String id) {
		sysDepartService.deleteEntityByDelFlag(id);
		return Result.ok("删除成功");
	}

	/**
	 * 批量删除 根据前端请求的多个ID,对数据库执行删除相关部门数据的操作
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
//	@RequiresPermissions("sys:depart:delete")
	@ApiOperation("批量删除组织机构部门")
	@AutoLog("批量删除组织机构部门")
	public Result deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		sysDepartService.deleteBatchEntityByDelFlag(ids);
		return Result.ok("删除成功");
	}

	/**
	 * 查询数据 添加或编辑页面对该方法发起请求,以树结构形式加载所有部门的名称,方便用户的操作
	 * 
	 * @return
	 */

	@RequestMapping(value = "/queryIdTree", method = RequestMethod.GET)
	@AutoLog(value="根据id查询组织机构部门树",logType=CommonConstant.LOG_TYPE_QUERY)
	public Result<List<DepartIdModel>> queryIdTree() {
		Result<List<DepartIdModel>> result = new Result<List<DepartIdModel>>();
		try {
			List<DepartIdModel> idList = FindsDepartsChildrenUtil.wrapDepartIdModel();
			if(idList.size()==0) {
				sysDepartService.queryTreeList();
				idList = FindsDepartsChildrenUtil.wrapDepartIdModel();
			}
			result.setResult(idList);
			result.setSuccess(true);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			return result;
		}
	}

	/**
	 * <p>
	 * 部门搜索功能方法,根据关键字模糊搜索相关部门
	 * </p>
	 * 
	 * @param keyWord
	 * @return
	 */
	@RequestMapping(value = "/searchBy", method = RequestMethod.GET)
	@AutoLog(value="根据关键字查询组织机构部门树",logType=CommonConstant.LOG_TYPE_QUERY)
	public Result<List<SysDepartTreeModel>> searchBy(@RequestParam(name = "keyWord", required = true) String keyWord) {
		Result<List<SysDepartTreeModel>> result = new Result<List<SysDepartTreeModel>>();
		try {
			List<SysDepartTreeModel> treeList = this.sysDepartService.searhBy(keyWord);
			if (treeList.size() == 0 || treeList == null) {
				throw new Exception();
			}
			result.setSuccess(true);
			result.setResult(treeList);
			return result;
		} catch (Exception e) {
			e.fillInStackTrace();
			result.setSuccess(false);
			result.setMessage("查询失败或没有您想要的任何数据!");
			return result;
		}
	}

}
